package com.maiyuren.fishkit.utils

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import com.maiyuren.fishkit.FishkitApplication
import org.json.JSONObject

object FishStorage {
    object keys {
        const val dict_sort = "dict_sord"
        const val user_import_dicts = "user_import_dicts"
        const val assets_file_pre = "assets_file_pre_"
        const val resource_file_pre = "resource_file_pre_"
        const val curr_dict_group_id = "curr_dict_group_id"
        const val curr_dict_group_tab_index = "curr_dict_group_tab_index"
        const val history_words = "history_words"
        const val favorite_words = "favorite_words"
    }

    private val sp: SharedPreferences by lazy {
        FishkitApplication.instance.getSharedPreferences("default", Context.MODE_PRIVATE)
    }
    fun getJson(key: String): JSONObject?{
        val str = sp.getString(key, null);
        if (str == null) return null
        return JSONObject(str);
    }

    fun getString(key: String): String? {
        return sp.getString(key, null)
    }

    fun setJson(key: String, value: JSONObject?) {
        setString(key, value?.toString() ?: "")
    }

    fun setString(key: String, value: String) {
        sp.edit {
            putString(key, value)
        }
    }

    fun setInt(key: String, value: Int) {
        sp.edit {
            putInt(key, value)
        }
    }

    fun getInt(key: String, default: Int = 0): Int {
        return sp.getInt(key, default)
    }

    fun setBoolean(key: String, value: Boolean) {
        sp.edit {
            putBoolean(key, value)
        }
    }

    fun getBoolean(key: String, default: Boolean = false): Boolean {
        return sp.getBoolean(key, default)
    }
}